import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _applyDecoratedDescriptor from "@babel/runtime/helpers/esm/applyDecoratedDescriptor";
var _excluded = ["code", "dataSource", "loading", "organizationId", "onListChange", "tenantsMulti"];

var _dec, _class;

/**
 * Table - 角色管理 - 列表页面表格
 * @date: 2018-7-4

 */
import React, { PureComponent } from 'react';
import { Bind } from 'lodash-decorators';
import { Badge, Table } from 'antd';
import { Button as ButtonPermission } from 'components/Permission';
import intl from 'utils/intl';
import { getCodeMeaning, isTenantRoleLevel, tableScrollWidth } from 'utils/utils';
import { VERSION_IS_OP } from 'utils/config';
import { operatorRender } from 'utils/renderer';
var List = (_dec = Bind(), (_class = /*#__PURE__*/function (_PureComponent) {
  _inherits(List, _PureComponent);

  var _super = _createSuper(List);

  function List() {
    var _this;

    _classCallCheck(this, List);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _super.call.apply(_super, [this].concat(args));
    _this.defaultTableRowKey = 'id';
    return _this;
  }

  _createClass(List, [{
    key: "optionsRender",
    value: function optionsRender(text, record) {
      var _this$props = this.props,
          _this$props$handleAct = _this$props.handleAction,
          handleAction = _this$props$handleAct === void 0 ? function (e) {
        return e;
      } : _this$props$handleAct,
          tenantRoleLevel = _this$props.tenantRoleLevel,
          path = _this$props.path;
      var operators = []; // assignedFlag   4 分配标志        -> 创建角色
      // adminFlag      2 管理标志
      // haveAdminFlag  1 有父级角色标志

      var branch = (record.assignedFlag === 1 ? 4 : 0) + (record.adminFlag === 1 ? 2 : 0) + (record.haveAdminFlag === 1 ? 1 : 0);
      var flag = false; // 创建角色
      // const createRoleBtn = {
      //   key: 'create',
      //   ele: (
      //     <ButtonPermission
      //       type="text"
      //       permissionList={[
      //         {
      //           code: `${path}.button.roleCreate`,
      //           type: 'button',
      //           meaning: '角色管理-创建角色',
      //         },
      //       ]}
      //       onClick={() => handleAction('create', record)}
      //     >
      //       {intl.get('ciam.roleManagement.view.button.roleCreate').d('创建角色')}
      //     </ButtonPermission>
      //   ),
      //   len: 4,
      //   title: intl.get('ciam.roleManagement.view.button.roleCreate').d('创建角色'),
      // };
      // 分配用户

      var assignMemberBtn = {
        key: 'assign-members',
        ele: /*#__PURE__*/React.createElement(ButtonPermission, {
          type: "text",
          permissionList: [{
            code: "".concat(path, ".button.members"),
            type: 'button',
            meaning: '角色管理-分配用户'
          }],
          onClick: function onClick() {
            return handleAction('editMembers', record);
          }
        }, intl.get("ciam.roleManagement.view.title.members").d('分配用户')),
        len: 4,
        title: intl.get("ciam.roleManagement.view.title.members").d('分配用户')
      }; // 分配用户

      var assignClientBtn = {
        key: 'assign-client',
        ele: /*#__PURE__*/React.createElement(ButtonPermission, {
          type: "text",
          permissionList: [{
            code: "".concat(path, ".button.clients"),
            type: 'button',
            meaning: '角色管理-分配客户端'
          }],
          onClick: function onClick() {
            return handleAction('editClients', record);
          }
        }, intl.get("ciam.roleManagement.view.title.clients").d('分配客户端')),
        len: 5,
        title: intl.get("ciam.roleManagement.view.title.clients").d('分配客户端')
      }; // 分配权限

      var assignPermission = {
        key: 'assign-permissions',
        ele: /*#__PURE__*/React.createElement(ButtonPermission, {
          type: "text",
          permissionList: [{
            code: "".concat(path, ".button.assignPermissions"),
            type: 'button',
            meaning: '角色管理-分配权限'
          }],
          onClick: function onClick() {
            return handleAction('assignPermissions', record);
          }
        }, intl.get("ciam.roleManagement.view.button.assignPermissions").d('分配权限')),
        len: 4,
        title: intl.get("ciam.roleManagement.view.button.assignPermissions").d('分配权限')
      }; // 复制

      var copyBtn = {
        key: 'copy',
        ele: /*#__PURE__*/React.createElement(ButtonPermission, {
          type: "text",
          permissionList: [{
            code: "".concat(path, ".button.copy"),
            type: 'button',
            meaning: '角色管理-复制'
          }],
          onClick: function onClick() {
            return handleAction('copy', record);
          }
        }, intl.get("cpaas.common.button.copy").d('复制')),
        len: 2,
        title: intl.get("cpaas.common.button.copy").d('复制')
      }; // 继承

      var inheritBtn = record.inheritable === 1 && {
        key: 'inherit',
        ele: /*#__PURE__*/React.createElement(ButtonPermission, {
          type: "text",
          permissionList: [{
            code: "".concat(path, ".button.inherit"),
            type: 'button',
            meaning: '角色管理-继承'
          }],
          onClick: function onClick() {
            return handleAction('inherit', record);
          }
        }, intl.get("ciam.roleManagement.view.title.button.inherit").d('继承')),
        len: 2,
        title: intl.get("ciam.roleManagement.view.title.button.inherit").d('继承')
      }; // 编辑

      var editBtn = {
        key: 'edit',
        ele: /*#__PURE__*/React.createElement(ButtonPermission, {
          type: "text",
          permissionList: [{
            code: "".concat(path, ".button.edit"),
            type: 'button',
            meaning: '角色管理-编辑'
          }],
          onClick: function onClick() {
            return handleAction('edit', record);
          }
        }, intl.get("cpaas.common.button.edit").d('编辑')),
        len: 2,
        title: intl.get("cpaas.common.button.edit").d('编辑')
      }; // 启用/禁用

      var enableBtn = {
        key: 'enabled',
        ele: /*#__PURE__*/React.createElement(ButtonPermission, {
          type: "text",
          permissionList: [{
            code: "".concat(path, ".button.status"),
            type: 'button',
            meaning: '角色管理-状态'
          }],
          onClick: function onClick() {
            return handleAction('enabled', record);
          }
        }, record.enabled ? intl.get("cpaas.common.status.disable").d('禁用') : intl.get("cpaas.common.status.enable").d('启用')),
        len: 2,
        title: record.enabled ? intl.get("cpaas.common.status.disable").d('禁用') : intl.get("cpaas.common.status.enable").d('启用')
      }; // 工作台配置-分配卡片

      var cardBtn = {
        key: 'assign-role',
        ele: /*#__PURE__*/React.createElement(ButtonPermission, {
          type: "text",
          permissionList: [{
            code: "".concat(path, ".button.assignCards"),
            type: 'button',
            meaning: '角色管理-工作台配置'
          }],
          onClick: function onClick() {
            return handleAction('assignCards', record);
          }
        }, intl.get("ciam.roleManagement.view.title.assignCards").d('工作台配置')),
        len: 5,
        title: intl.get("ciam.roleManagement.view.title.assignCards").d('工作台配置')
      };
      /* eslint-disable no-fallthrough */

      switch (branch) {
        case 6:
          // 分配&管理
          if (flag) {
            operators.push( // createRoleBtn,
            assignMemberBtn, assignClientBtn);
          } else {
            operators.push( // createRoleBtn,
            copyBtn, inheritBtn, assignMemberBtn, assignClientBtn);
          }

          break;

        case 7: //   // 分配&管理&父级管理角色
        //   operators.push(createRoleBtn);

        case 5: // 分配&父级管理角色

        case 1:
          // 父级管理角色
          if (flag) {
            operators.push(editBtn, enableBtn, assignMemberBtn, assignClientBtn);
          } else {
            operators.push(copyBtn, inheritBtn, editBtn, enableBtn, assignMemberBtn, assignClientBtn);
          }

          if (record.enabled) {
            // 角色启用
            operators.push(assignPermission);
          }

          break;

        case 4: // 分配

        default:
          return null;
      }
      /* eslint-enable no-fallthrough */


      if (record.enabled) {
        operators.push(cardBtn);
      } // 只有 `创建角色`,`复制`,`继承`,`编辑` 在外面 其他的操作都在 操作下拉菜单中


      var operatorLimit = 4;

      if (branch === 6) {
        operatorLimit = 3;
      }

      var newOperators = operators.filter(Boolean);
      return operatorRender(newOperators, record, {
        limit: operatorLimit
      });
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props2 = this.props,
          code = _this$props2.code,
          _this$props2$dataSour = _this$props2.dataSource,
          dataSource = _this$props2$dataSour === void 0 ? [] : _this$props2$dataSour,
          loading = _this$props2.loading,
          organizationId = _this$props2.organizationId,
          _this$props2$onListCh = _this$props2.onListChange,
          onListChange = _this$props2$onListCh === void 0 ? function (e) {
        return e;
      } : _this$props2$onListCh,
          tenantsMulti = _this$props2.tenantsMulti,
          others = _objectWithoutProperties(_this$props2, _excluded);

      var isTenant = isTenantRoleLevel();
      var columns = [{
        dataIndex: 'name',
        title: intl.get("ciam.roleManagement.model.roleManagement.name").d('角色名称'),
        width: 200
      }, {
        dataIndex: 'code',
        title: intl.get("ciam.roleManagement.model.roleManagement.code").d('角色编码'),
        width: isTenant ? 300 : 150
      }, !VERSION_IS_OP && !isTenant && {
        dataIndex: 'levelMeaning',
        title: intl.get("ciam.roleManagement.model.roleManagement.level").d('角色层级'),
        width: 120
      }, // {
      //   dataIndex: 'parentRoleName',
      //   title: intl.get('ciam.roleManagement.model.roleManagement.topRole').d('上级角色'),
      //   width: 150,
      // },
      !isTenant && {
        dataIndex: 'roleSource',
        title: intl.get("ciam.roleManagement.model.roleManagement.roleSource").d('角色来源'),
        width: 120,
        render: function render(text) {
          return getCodeMeaning(text, code);
        }
      }, !VERSION_IS_OP && (!isTenant || tenantsMulti) && {
        dataIndex: 'tenantName',
        title: intl.get("ciam.roleManagement.model.roleManagement.tenant").d('所属租户'),
        width: 150
      }, {
        dataIndex: 'inheritedRoleName',
        title: intl.get("ciam.roleManagement.model.roleManagement.inheritedRole").d('继承自'),
        width: 150
      }, {
        dataIndex: 'enabled',
        title: intl.get("cpaas.common.status").d('状态'),
        width: 90,
        render: function render(text, record) {
          return /*#__PURE__*/React.createElement(Badge, {
            status: record.enabled ? 'success' : 'error',
            text: record.enabled ? intl.get("cpaas.common.status.enable").d('启用') : intl.get("ciam.roleManagement.view.title.disable").d('禁用')
          });
        }
      }, // {
      //   dataIndex: 'assignedFlag',
      //   title: intl.get('ciam.roleManagement.model.roleManagement.assignedFlag').d('分配标志'),
      //   width: 100,
      //   render: assignedRole => {
      //     if (assignedRole) {
      //       return <Tag color="green">{intl.get('cpaas.common.status.yes').d('是')}</Tag>;
      //     }
      //     return <Tag color="orange">{intl.get('cpaas.common.status.no').d('否')}</Tag>;
      //   },
      // },
      // {
      //   dataIndex: 'adminFlag',
      //   title: intl.get('ciam.roleManagement.model.roleManagement.adminFlag').d('管理标志'),
      //   width: 100,
      //   render: adminRole => {
      //     if (adminRole) {
      //       return <Tag color="green">{intl.get('cpaas.common.status.yes').d('是')}</Tag>;
      //     }
      //     return <Tag color="orange">{intl.get('cpaas.common.status.no').d('否')}</Tag>;
      //   },
      // },
      // {
      //   dataIndex: 'adminRoleName',
      //   title: intl.get('ciam.roleManagement.model.roleManagement.adminRole').d('父级管理角色'),
      //   width: 150,
      //   render: (adminRoleName, record) => {
      //     if (record.haveAdminFlag) {
      //       return adminRoleName;
      //     }
      //   },
      // },
      {
        dataIndex: 'createdUserName',
        title: intl.get('ciam.roleManagement.model.roleManagement.createdUserName').d('创建人'),
        width: 200
      }, {
        dataIndex: 'levelPath',
        title: intl.get('ciam.roleManagement.model.roleManagement.levelPath').d('角色路径')
      }, {
        key: 'operator',
        title: intl.get('cpaas.common.button.action').d('操作'),
        width: 290,
        fixed: 'right',
        render: this.optionsRender
      }].filter(Boolean);

      var tableProps = _objectSpread({
        dataSource: dataSource,
        loading: loading,
        columns: columns,
        pagination: false,
        bordered: true,
        childrenColumnName: 'childRoles',
        rowKey: this.defaultTableRowKey,
        scroll: {
          x: tableScrollWidth(columns)
        },
        onChange: onListChange
      }, others);

      return /*#__PURE__*/React.createElement(Table, tableProps);
    }
  }]);

  return List;
}(PureComponent), (_applyDecoratedDescriptor(_class.prototype, "optionsRender", [_dec], Object.getOwnPropertyDescriptor(_class.prototype, "optionsRender"), _class.prototype)), _class));
export default List;